package bat.iog.hytool.future.CompletableFuture;

import java.util.concurrent.CompletableFuture;

public class CompletableFutureDemo {

    public static void main(String[] args) throws InterruptedException {
        CompletableFuture.supplyAsync(CompletableFutureDemo::getPrice).thenAccept(
                result -> {
                    System.out.println("price: " + result);
                }
        ).exceptionally(
                e -> {
                    e.printStackTrace();
                    return null;
                }
        );
        Thread.sleep(200);
    }
    
    public static Double getPrice() {

        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        if (Math.random() < 0.3) {
            throw new RuntimeException("Error when get price");
        }

        return Math.random() * 20;
    }
}
